{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "source": [
    "import cv2\n",
    "import numpy as np"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 形态学-腐蚀操作"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "source": [
    "img = cv2.imread('./dige.png')\n",
    "\n",
    "cv2.imshow('img', img)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "source": [
    "kernel = np.ones((5, 5), np.uint8)\n",
    "erosion = cv2.erode(img, kernel, iterations=1)\n",
    "\n",
    "cv2.imshow('erosion', erosion)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "source": [
    "pie = cv2.imread('./pie.png')\n",
    "\n",
    "cv2.imshow('pie', pie)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "source": [
    "kernel = np.ones((30, 30), np.uint8)\n",
    "erosion_1 = cv2.erode(pie, kernel, iterations=1)\n",
    "erosion_2 = cv2.erode(pie, kernel, iterations=2)\n",
    "erosion_3 = cv2.erode(pie, kernel, iterations=3)\n",
    "\n",
    "res = np.hstack((erosion_1, erosion_2, erosion_3))\n",
    "cv2.imshow('res', res)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 形态学-膨胀操作"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "source": [
    "img = cv2.imread('./dige.png')\n",
    "\n",
    "cv2.imshow('img', img)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "source": [
    "kernel = np.ones((3, 3), np.uint8)\n",
    "dige_erosion = cv2.erode(img, kernel, iterations=1)\n",
    "\n",
    "cv2.imshow('erosion', dige_erosion)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "source": [
    "kernel = np.ones((3, 3), np.uint8)\n",
    "dige_dilate = cv2.dilate(dige_erosion, kernel, iterations=1)\n",
    "\n",
    "cv2.imshow('dilate', dige_dilate)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "source": [
    "pie = cv2.imread('./pie.png')\n",
    "\n",
    "kernel = np.ones((30, 30), np.uint8)\n",
    "erosion_1 = cv2.dilate(pie, kernel, iterations=1)\n",
    "erosion_2 = cv2.dilate(pie, kernel, iterations=2)\n",
    "erosion_3 = cv2.dilate(pie, kernel, iterations=3)\n",
    "\n",
    "res = np.hstack((erosion_1, erosion_2, erosion_3))\n",
    "cv2.imshow('res', res)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 梯度运算"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "source": [
    "# 梯度 = 膨胀 - 腐蚀\n",
    "pie = cv2.imread('./pie.png')\n",
    "kernel = np.ones((7, 7), np.uint8)\n",
    "dilate = cv2.dilate(pie, kernel, iterations=5)\n",
    "erosion = cv2.erode(pie, kernel, iterations=5)\n",
    "\n",
    "res = np.hstack((dilate, erosion))\n",
    "\n",
    "cv2.imshow('res', res)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "source": [
    "gradient = cv2.morphologyEx(pie, cv2.MORPH_GRADIENT, kernel)\n",
    "\n",
    "cv2.imshow('gradient', gradient)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 开运算与闭运算"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "source": [
    "# 开：先腐蚀，再膨胀\n",
    "img = cv2.imread('./dige.png')\n",
    "\n",
    "kernel = np.ones((5, 5), np.uint8)\n",
    "opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)\n",
    "\n",
    "cv2.imshow('opening', opening)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "source": [
    "# 闭：先膨胀，再腐蚀\n",
    "img = cv2.imread('./dige.png')\n",
    "\n",
    "kernel = np.ones((5, 5), np.uint8)\n",
    "closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)\n",
    "\n",
    "cv2.imshow('closing', closing)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "source": [
    "res = np.hstack((opening, closing))\n",
    "\n",
    "cv2.imshow('res', res)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 礼帽与黑帽\n",
    "\n",
    "- 礼帽 = 原始输入 - 开运算结果\n",
    "- 黑帽 = 闭运算结果 - 原始输入"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "source": [
    "# 礼帽\n",
    "img = cv2.imread('./dige.png')\n",
    "\n",
    "tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)\n",
    "cv2.imshow('tophat', tophat)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "source": [
    "# 黑帽\n",
    "img = cv2.imread('./dige.png')\n",
    "\n",
    "blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)\n",
    "cv2.imshow('blackhat', blackhat)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.8.10",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.10 64-bit ('.venv': pipenv)"
  },
  "interpreter": {
   "hash": "f3b91acf9723cb036efcf4de6fbdb9b4d8464c97d04542249f4ab60120686619"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}